package cn.jiahua.hospital.controller.adminBack;

import cn.jiahua.hospital.dto.HospitalDto;
import cn.jiahua.hospital.dto.Page;
import cn.jiahua.hospital.dto.Result;
import cn.jiahua.hospital.pojo.Hospital;
import cn.jiahua.hospital.pojo.User;
import cn.jiahua.hospital.service.HospitalService;
import cn.jiahua.hospital.utils.MathUtils;
import cn.jiahua.hospital.utils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@RestController
@Api(tags = "超级管理员后台-医院")
@RequestMapping("/api/back/hospitala/")
public class HospitalBackController {

    @Autowired
    HospitalService hospitalService;



    @ApiOperation(value = "未禁用医院分页", produces = "application/json", notes = "根据医院名称和医院地址模糊查询,当前页码和页容量分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hospitalName", value = "医院姓名", required = false, dataType = "string"),
            @ApiImplicitParam(name = "AddressId", value = "医院地址 ", required = false, dataType = "int"),
            @ApiImplicitParam(name = "page", value = "当前页码 ", required = true, dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "页大小 ", required = true, dataType = "int")
    })
    @GetMapping("/limit")
    public Result limit(String hospitalName,
                        Integer AddressId,
                        Integer page,
                        Integer limit) {

        //总数量
        int count = hospitalService.hospitalSelectCount2(hospitalName, AddressId);
        System.out.println(count);
        Page<User> page1 = new Page<User>();
        page1.setTotal(count);
        page1.setPageNo(page);
        page1.setPageSize(limit);
        //总页数
        int pageCount = page1.getPages();
        //调用service层获取数据
        List<HospitalDto> hospitalsList = hospitalService.hospitalSelectPage2(hospitalName, AddressId, (page - 1) * limit, limit);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("page1", page1);
        map.put("userList", hospitalsList);
        return ResultUtils.returnSuccess("查询成功", map);
    }

    @ApiOperation(value = "禁用医院分页", produces = "application/json", notes = "根据医院名称和医院地址模糊查询,当前页码和页容量分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hospitalName", value = "医院姓名", required = false, dataType = "string"),
            @ApiImplicitParam(name = "AddressId", value = "医院地址 ", required = false, dataType = "int"),
            @ApiImplicitParam(name = "page", value = "当前页码 ", required = true, dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "页大小 ", required = true, dataType = "int")
    })
    @GetMapping("/stop/limit")
    public Result stopLimit(String hospitalName,
                        Integer AddressId,
                        Integer page,
                        Integer limit) {

        //总数量

        int count = hospitalService.hospitalSelectCount3(hospitalName, AddressId);
        System.out.println(count);
        Page<User> page1 = new Page<User>();
        page1.setTotal(count);
        page1.setPageNo(page);
        page1.setPageSize(limit);
        //总页数
        int pageCount = page1.getPages();
        //调用service层获取数据
        List<HospitalDto> hospitalsList = hospitalService.hospitalSelectPage3(hospitalName, AddressId, (page - 1) * limit, limit);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("page1", page1);
        map.put("userList", hospitalsList);
        return ResultUtils.returnSuccess("查询成功", map);
    }

    @ApiOperation(value = "查看医院", produces = "application/json", notes = "根据医院ID查看医院详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hospitalId", value = "医院ID", required = true, dataType = "int")
    })
    @GetMapping("/select")
    public Result select(Integer hospitalId) {
        Hospital hospital = hospitalService.getHospitalById(hospitalId);
        HospitalDto hospitalDto = hospitalService.getHospitalDtoById(hospitalId);
        HospitalDto hospitalDto2 = hospitalService.getHospitalDtoById2(hospitalId);
        Map<String, Object> map = new HashMap<>();
        map.put("hospital",hospital);
        map.put("hospitalDto",hospitalDto);
        map.put("hospitalDto2",hospitalDto2);
        return ResultUtils.returnDataSuccess(map);
    }




    @ApiOperation(value = "删除医院", produces = "application/json", notes = "根据医院ID删除医院信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hospitalId", value = "医院ID", required = true, dataType = "int"),
            @ApiImplicitParam(name = "AddressId", value = "地区ID", required = true, dataType = "int")
    })
    @GetMapping("/black")
    public Result black(Integer hospitalId,Integer AddressId) {
        Hospital hospital = new Hospital();
        hospital.setHospitalId(hospitalId);
        int AddressIds=0;
        if(AddressId>90){
            AddressIds=AddressId-90;
        }else if(hospitalId<90){
            AddressIds=AddressId+90;
        }
        hospital.setAddressId(AddressIds);
        int count = hospitalService.qdtxModifyHospital(hospital);
        if (count > 0) {
            return ResultUtils.returnSuccess("修改成功");
        }
        return ResultUtils.returnFail();
    }


    @ApiOperation(value = "注册医院", produces = "application/json", notes = "根据医院名称随机生成账号密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hospitalName", value = "医院名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "hospitalphone", value = "座机电话", required = true, dataType = "String"),
            @ApiImplicitParam(name = "hospitalTypeId", value = "医院类型id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "hospitalgradeId", value = "医院等级id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "AddressId", value = "地区id", required = true, dataType = "int"),
    })
    @GetMapping("/add")
    public Result add(String hospitalName,
                      String hospitalphone,
                      Integer hospitalTypeId,
                      Integer hospitalgradeId,
                      Integer AddressId) {
        boolean flag = true;
        //判断医院是否存在
        Hospital hospital = new Hospital();
        Map<String, Object> map = new HashMap<>();
        map.put("hospitalName", hospitalName);
        List<Hospital> hospitalList = hospitalService.getHospitalListByMap(map);
        //如果等于0医院不存在
        if (hospitalList.size() == 0) {
            hospital.setHospitalName(hospitalName);
            //判断随机生成的账号是否存在
            do {
                String account = MathUtils.random2();
                Map<String, Object> map1 = new HashMap<>();
                map1.put("account", account);
                List<Hospital> hospitalList1 = hospitalService.getHospitalListByMap(map1);
                if (hospitalList1.size() == 0) {
                    String password = MathUtils.random3();
                    hospital.setAccount(account);
                    hospital.setPassword(password);
                    hospital.setHospitalphone(hospitalphone);
                    hospital.setHospitalgradeId(hospitalgradeId);
                    hospital.setHospitalTypeId(hospitalTypeId);
                    hospital.setAddressId(AddressId);
                    int count2 = hospitalService.qdtxAddHospital(hospital);
                    if (count2 > 0) {
                        flag = true;
                        return ResultUtils.returnDataSuccess(hospital);
                    } else {
                        flag = false;
                    }
                }
            } while (!flag);
            /*}*/
        } else {
            return ResultUtils.returnFail("该医院名已存在,无法注册");
        }
        return ResultUtils.returnFail();
    }
}
